<?php

class Widget_JQueryScrollImage extends WP_Widget {

	function Widget_JQueryScrollImage() {
		$widget_ops = array( 'classname' => 'widget_jquery_scroll', 'description' => __( "jQuery Scroll Image" ) );
		parent::WP_Widget( false, __( 'jQuery Scroll Image', 'woothemes' ), $widget_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'jQuery Scroll Image' ) : $instance['title'], $instance, $this->id_base);
        $numprod = $instance['numprod']?$instance['numprod']:5;
        $post_type = $instance['post_type'];
        $image_w = $instance['image_w']?$instance['image_w']:150;
        $image_h = $instance['image_h']?$instance['image_h']:150;
        $time_delay = $instance['time_delay']?$instance['time_delay']:1000;
        $scroll = $instance['scroll']?$instance['scroll']:2;
        
		echo $before_widget;
		if ( $title )
			echo $before_title . $title . $after_title;
          
          
        $the_query = new WP_Query(array(
            'post_type' => $post_type,
            'orderby' => 'date',
            'order' => 'desc',
            'status' => array('publish','private'),
            'posts_per_page' => $numprod
        ));
        if($the_query->have_posts()) { ?>
         <div id="<?php echo $this->id;?>" class="jcarousel-skin-name">
          <div class="jcarousel-container">
            <div class="jcarousel-clip">
                <ul class="product-news jcarousel-list">
                <?php while($the_query->have_posts()) { $the_query->the_post(); ?>
                    <li> 
                        <?php 
                        $link = get_post_meta(get_the_ID(),"link",true);
                        if($link=='') $link = get_permalink();
                        ?>
                        
                        <a href="<?php echo $link;?>">
                            <?php if(has_post_thumbnail()) {
                                list($url,$width, $height,$icon) = wp_get_attachment_image_src(get_post_thumbnail_id(),"full");
                                ?>
                                <img src="<?php echo get_template_directory_uri();?>/functions/thumb.php?src=<?php echo $url;?>&w=<?php echo $image_w;?>&h=<?php echo $image_h;?>&zc=2&a=c&cc=ffffff" />
                                <?                                    
                            }else{?>
                                <img style="width:<?php echo $image_w;?>px; height:<?php echo $image_h;?>px;" class="attachment-thumbnail wp-post-image no-image" src="<?php echo noimage_src();?>"  />
                            <?php } ?>
                        </a>
                    </li>  
                <?php } wp_reset_query(); ?>
                </ul>
            </div>
          </div>
         </div>
        <?php }
		echo $after_widget;
        ?>
       <script type='text/javascript'>
        /* <![CDATA[ */
        	jQuery(function ( $ ){
        	   // scroll image special
                $('#<?php echo $this->id;?>').jcarousel({
                    vertical:true,
                    scroll: <?php echo $scroll;?>,
                    auto:5,
                    wrap: 'last',
                    animation: <?php echo $time_delay;?>
                }); 
        	});
        /* ]]> */
        </script>
        <style>
        <?php echo "#".$this->id;?> .jcarousel-clip ul li{
            height: <?php echo $image_h;?>px;
        }
        
        <?php echo "#".$this->id;?> .jcarousel-clip{
            height: <?php echo $image_h*$scroll;?>px;
        }
        </style>
       <?php 
           
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
        $instance['numprod'] = strip_tags($new_instance['numprod']);
        $instance['post_type'] = strip_tags($new_instance['post_type']);
        $instance['image_w']= strip_tags($new_instance['image_w']);
        $instance['image_h']= strip_tags($new_instance['image_h']);
        $instance['time_delay']= strip_tags($new_instance['time_delay']);
        $instance['scroll']= strip_tags($new_instance['scroll']);
		return $instance;
	}

	function form( $instance ) {
		//Defaults
		$instance = wp_parse_args( (array) $instance, array( 
            'title' => '',
            'numprod'=>5, 
            'post_type' => 'post',
            'image_w' => 150,
            'image_h' => 150,
            'time_delay' => 1000,
            'scroll' => 2 ) );
            
		$title = esc_attr( $instance['title'] );
        $numprod = esc_attr( $instance['numprod'] );
        $post_type = esc_attr( $instance['post_type'] );
        $image_w = esc_attr( $instance['image_w'] );
        $image_h = esc_attr( $instance['image_h'] );
        $time_delay = esc_attr( $instance['time_delay'] );
        $scroll = esc_attr( $instance['scroll'] );
        
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
        
        <p><label for="<?php echo $this->get_field_id('numprod'); ?>"><?php _e( 'Số lượng:' ); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('numprod'); ?>" name="<?php echo $this->get_field_name('numprod'); ?>" type="text" value="<?php echo $numprod; ?>" /></p>
        
        <p><label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e( 'Post type:' ); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>" type="text" value="<?php echo $post_type; ?>" /></p>
        
        <p><label for="<?php echo $this->get_field_id('image_w'); ?>"><?php _e( 'WxH:' ); ?></label>
		<input size="10" id="<?php echo $this->get_field_id('image_w'); ?>" name="<?php echo $this->get_field_name('image_w'); ?>" type="text" value="<?php echo $image_w; ?>" /> x 
        <input size="10" id="<?php echo $this->get_field_id('image_h'); ?>" name="<?php echo $this->get_field_name('image_h'); ?>" type="text" value="<?php echo $image_h; ?>" /></p>
        
        <p><label for="<?php echo $this->get_field_id('time_delay'); ?>"><?php _e( 'Thời gian chạy:' ); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('time_delay'); ?>" name="<?php echo $this->get_field_name('time_delay'); ?>" type="text" value="<?php echo $time_delay; ?>" /></p>
        
        <p><label for="<?php echo $this->get_field_id('scroll'); ?>"><?php _e( 'Scroll:' ); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('scroll'); ?>" name="<?php echo $this->get_field_name('scroll'); ?>" type="text" value="<?php echo $scroll; ?>" /></p>
<?php
	}
    
    

}

register_widget( 'Widget_JQueryScrollImage' );


?>